package thisgame;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.net.*;
import java.util.List;
import thisgame.model.*;
import thisgame.model.Character;
import thisgame.model.Direction;

public class GamePlay extends Frame implements KeyListener {
	private Character cha;
	private boolean enterOn = true;
	private Panel textPanel;
	private Canvas canvas;

	private BufferedInputStream io;
	private BufferedOutputStream out;
	
	private ThisMap map;

	private TextField sayText;
	private Socket socket;
	
	private TextArea viewText;
	
	public GamePlay() {
		this.init();
	}
	
	public void init() {
		// 캐릭터 메이킹.
		cha = new Character(new Ch().getName(), 40, 22);
		
		 try { 
			 socket = new Socket("203.241.228.67", 5555);
			 io = new BufferedInputStream(socket.getInputStream());
			 out = new BufferedOutputStream(socket.getOutputStream());
		 } catch (Exception e) { System.out.println("서버에 접속 실패"); }
		 
		this.setBounds(0, 0, 800, 800);
		this.addWindowListener(new WinEvent());
		
		canvas = new Canvas() {
			public void paint(Graphics g){
				List<Drawable> ds = map.provide(g, new Rectangle(getSize()),
												cha.getPosition(), this);
				for (Drawable d : ds) d.draw();
				cha.paintTo(g, new Point(380, 200), this);
			}
		};
		
		textPanel = new Panel(new BorderLayout());
		// Text Setting
		sayText = new TextField();
		viewText = new TextArea();
		viewText.setText(cha.getName() + " 님이 접속했습니다.\n");
		// add on

		textPanel.add(viewText, BorderLayout.NORTH);
		textPanel.add(sayText, BorderLayout.SOUTH);
		sayText.addKeyListener(this);
		this.add(canvas, BorderLayout.CENTER);
		this.add(textPanel, BorderLayout.SOUTH);
		this.setVisible(true);
		this.setFocusable(true);
		this.addKeyListener(this);
		
		// load a map
		map = new ThisMap();
		
		loading();
		start();
	}
	
	public void sayMsg(byte[] buf){
		try{
			out.write(buf);
			out.flush();
		}catch(Exception e){}
	}

	public void keyPressed(KeyEvent e) {
		if (!enterOn) {
			switch (e.getKeyCode()) {
			case KeyEvent.VK_ENTER:
				enterOn = true;
				this.requestFocus();
				viewText.append(cha.getName() + " : " + sayText.getText() + "\n");
				sayMsg((sayText.getText()+"\n").getBytes());
				sayText.setText("");
				System.out.println("enter");
				break;
			}
		} else {
			switch (e.getKeyCode()) {
			case KeyEvent.VK_UP:
				cha.move(Direction.UP);
				System.out.println("up");
				break;
			case KeyEvent.VK_DOWN:
				cha.move(Direction.DOWN);
				System.out.println("down");
				break;

			case KeyEvent.VK_RIGHT:
				cha.move(Direction.RIGHT);
				System.out.println("right");
				break; 

			case KeyEvent.VK_LEFT:
				cha.move(Direction.LEFT);
				System.out.println("left");
				break;

			case KeyEvent.VK_ENTER:
				enterOn = false;
				sayText.requestFocus();
				System.out.println("enter");
				break;
			}
			System.out.println(cha.getPosition());
			canvas.repaint();
		}
		// TODO Auto-generated method stub

	}

	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	public void loading() {

	}

	public void start() {
		repaint();
	}
}
